package com.example.ui.contact.db;

import java.util.Collections;
import java.util.LinkedList;

import com.example.ui.contact.bean.BaseItem;
import com.example.ui.contact.bean.CommentItem;
import com.example.ui.contact.bean.MeItem;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class CommentMsgDB {
	public static final String MSG_DBNAME = "Msg.db";
	private static final String CommentMsg_TABLE_NAME = "commentMsg";
	private SQLiteDatabase db;

	@SuppressWarnings("static-access")
	public CommentMsgDB(Context context){
		db = context.openOrCreateDatabase(MSG_DBNAME,Context.MODE_APPEND,null);
		db.execSQL("CREATE table IF NOT EXISTS "
				+ CommentMsg_TABLE_NAME
				+ " (CommentMsg_TABLE_NAME INTEGER PRIMARY KEY AUTOINCREMENT,content TEXT, userId TEXT," +
				"nickname TEXT,typeId TEXT,communityId TEXT,txt TEXT,dateline TEXT ,location TEXT,name TEXT,pic TEXT)");

		
	}


	
	public void saveCommentMsg(CommentItem item){


		
		db.execSQL(
				"insert into "
						+ CommentMsg_TABLE_NAME
						+ " (content,userId,nickname,typeId,communityId,txt,dateLine,location,name,pic) "
						+"values(?,?,?,?,?,?,?,?,?,?)",
				new Object[] { item.getContent(), item.getUserId(),item.getNick(),item.getType(),item.getCommunityId(),
							   item.getTxt(), item.getDateLine(),item.getLocation(),item.getName(),item.getHeadPic()});

	}

	public LinkedList<CommentItem> getCommentMsgList() {
		LinkedList<CommentItem> list = new LinkedList<CommentItem>();
		Cursor c = db.rawQuery("SELECT * from " + CommentMsg_TABLE_NAME, null);
		while (c.moveToNext()) {
			String content = c.getString(c.getColumnIndex("content"));
			String userId = c.getString(c.getColumnIndex("userId"));
			String nickname = c.getString(c.getColumnIndex("nickname"));
			String typeId = c.getString(c.getColumnIndex("typeId"));			
			String communityId = c.getString(c.getColumnIndex("communityId"));
			String txt = c.getString(c.getColumnIndex("txt"));
			String dateLine = c.getString(c.getColumnIndex("dateline"));
			String location = c.getString(c.getColumnIndex("location"));
			String name = c.getString(c.getColumnIndex("name"));
			String headPic = c.getString(c.getColumnIndex("pic"));

			CommentItem item = new CommentItem();
			item.setContent(content);
			item.setUserId(userId);
			item.setNick(nickname);
			item.setType(typeId);
			item.setCommunityId(communityId);
			item.setTxt(txt);
			item.setDateLine(dateLine);
			item.setLocation(location);					
			item.setName(name);
			item.setHeadPic(headPic);			
			list.add(item);
		}
		//Collections.sort(list);// 按时间降序
		return list;
	}
	
	public void close() {
		if (db != null)
			db.close();
	}
	
	
	private boolean isExist(String userId) {
		Cursor c = db.rawQuery("SELECT * FROM " + MSG_DBNAME
				+ " WHERE userId = ?", new String[] { userId });
		return c.moveToFirst();
	}	
	
}
